package com.javaeye.chenweihuacwh.service.baseData;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.javaeye.chenweihuacwh.dao.baseData.ProductCategoryDAO;
import com.javaeye.chenweihuacwh.dao.baseData.ProductDAO;
import com.javaeye.chenweihuacwh.dto.baseData.ProductCategory;

public class ProductCategoryService implements IProductCategoryService {
	
	private static Log log = LogFactory.getLog(ProductCategoryService.class);
	
	private ProductCategoryDAO dao;
	
	private ProductDAO productDao;
	
	public void setDao(ProductCategoryDAO dao) {
		this.dao = dao;
	}
	
	public void setProductDao(ProductDAO productDao) {
		this.productDao = productDao;
	}

	@Override
	public ProductCategory getCategory(String id) {
		int intId = Integer.parseInt(id);
		ProductCategory category = dao.getCategory(intId);
		if (category == null) {
			log.warn("在数据库中未找到指定的产品分类  id：" + id);
		}
		return category;
	}

	@Override
	public List getCategorys() {
		List results = dao.getCategorys();
		if (results == null || results.size() == 0) {
			log.warn("在数据库中未找到产品分类数据");
		}
		return results;
	}

	@Override
	public boolean removeCategory(String id) {
		int intId = Integer.parseInt(id);
		//TODO 判断产品表和客户表中是否有对应的记录
		//如果有，则不能删除
		long count = productDao.getProductCountByCategoryId(intId);
		if (count > 0) 
			return false;
		dao.removeCategory(intId);
		return true;
	}

	@Override
	public ProductCategory saveCategory(ProductCategory category) {
		dao.saveCategory(category);
		return category;
	}

}
